package com.vancouver.knows.util;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.appsflyer.AppsFlyerLib;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.business.BSharedPrefManager;

import android.content.Context;

public class AfTrackerUtil {

	public static void setAppsFlyer(Context context,String uid) {
		BSharedPrefManager bSharedPrefManager = ((MainApplication)context.getApplicationContext()).getSharedPrefManager();
		if (!bSharedPrefManager.getFirstLoginSign()) {
			//首次登录设置af, 状态设为true
			bSharedPrefManager.saveIsFirstLogin(true);
			appsFlyer(true, context, uid);
		}else {
			appsFlyer(false, context ,uid);
		}
	}
	
	private static void appsFlyer(boolean isLoadFirst, Context context,String userId){
		//没有渠道信息时加载AF追踪
		String devKey = "QHx4Q9CSjt3xhgTcZ7BYSd";
		String gameId = "1000000000";
		
		AppsFlyerLib.setAppsFlyerKey(devKey);
		AppsFlyerLib.sendTracking(context);
 
		JSONObject jsonObject = new JSONObject();
		String customerUserData = "";
		try {
			jsonObject.put("g_id", gameId);
		    customerUserData = jsonObject.toString();
		} catch (Exception e) {
			
		}
		AppsFlyerLib.setCustomerUserId(customerUserData);
		if (isLoadFirst){
			sendSignUpEvent(context,userId);
		}
	}
	
	private static void sendSignUpEvent(Context context,String userId){
	    Map<String, Object> eventValue = new HashMap<String, Object>();
	    eventValue.put("userid", userId); 
	    //发送自定义的 SIGN_UP 事件
	    AppsFlyerLib.trackEvent(context, "SIGN_UP", eventValue);
		
	}
}